/*
 * Processor.h
 *
 *  Created on: Oct 25, 2011
 *      Author: zwx
 */

#ifndef PROCESSOR_H_
#define PROCESSOR_H_
#include <iostream>
#include "../Include/includes.h"
using namespace std;

struct term_info {
	size_t id;
	size_t df;
	size_t offset;
};
struct doc_info {
	size_t length; //token wise length of doc
	size_t offset; //offset in the file dictionary
	int ref_count; //number of reference
	int sec_count; //number of section
};
struct aut_cat{
	size_t	id;
	size_t	offset;
};
class Processor {
private:
	static Processor* prc;
	static bool instanceFlag;
	static const int MAX_LINE_SIZE = 65536;
	static const int MAX_DOC_NUM = 131072;
	char _static_score_path[MAX_LINE_SIZE];
	char _data_path[MAX_LINE_SIZE];
	char _config_path[MAX_LINE_SIZE];
	char _index_path[MAX_LINE_SIZE];
	char _input_path[MAX_LINE_SIZE];
	char _output_path[MAX_LINE_SIZE];
	int _file_dict_barrel_size;
	int _inv_index_barrel_size;
	int _doc_number;
	double static_score[MAX_DOC_NUM];
	map<string, struct term_info> term_map;
	vector<string> doc_name;
	map<size_t, struct doc_info> doc_map; //doc_id, doc_info
	set<string> stopwords;
	map<string, struct aut_cat> aut_map;
	map<string, struct aut_cat> cat_map;
public:
	Processor();
	virtual ~Processor();
	static Processor* getInstance();
	void process(char*, char*, int, int, int);
	void load_config();
	void load_term_map();
	void load_doc_map();
	void load_stopwords();
	void load_doc_name();
	void load_author_map();
	void load_category_map();
	void load_static_score();
	void set_term_map(string term, size_t id, size_t offset);
	void set_doc_map(size_t id, struct doc_info);
	void set_stopwords(string word);
	void set_aut_map(string, struct aut_cat);	//parameter: author name, offset in the index
	void set_cat_map(string, struct aut_cat);	//parameter: category name, offset in the index
	void split_line(char* line, char* term, size_t& id);
	void split_line(char* line, struct doc_info& doc);
	bool split_line_2(char* line, char* term, size_t& id);
	bool is_stopword(string word);
	int get_term_info(string term, struct term_info& t);
	struct doc_info* get_doc_map(size_t id);
	string get_doc_name(size_t id);
	inline map<string, struct aut_cat>* get_aut_map() {
		return &aut_map;
	}
	inline map<string, struct aut_cat>* get_cat_map() {
		return &cat_map;
	}
	inline int get_doc_number() const {
		return _doc_number;
	}
	inline void set_doc_number(int _doc_number) {
		this->_doc_number = _doc_number;
	}
	inline map<string, struct term_info>* get_term_map() {
		return &term_map;
	}
	inline char* get_config_path() {
		return _config_path;
	}
	inline char* get_index_path() {
		return _index_path;
	}
	inline char* get_input_path() {
		return _input_path;
	}
	inline char* get_output_path() {
		return _output_path;
	}
	inline int get_file_dict_barrel_size() {
		return _file_dict_barrel_size;
	}
	inline int get_inv_index_barrel_size() {
		return _inv_index_barrel_size;
	}
	inline void set_doc_name(string name) {
		doc_name.push_back(name);
	}
	inline double* get_static_score() {
		return static_score;
	}
};

#endif /* PROCESSOR_H_ */
